package com.sk.weichat.view;

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.widget.Toast;

import com.alibaba.security.rp.RPSDK;
import com.luozm.captcha.Captcha;
import com.sk.weichat.R;
import com.sk.weichat.util.ToastUtil;

public class CaptchaDialog extends AlertDialog {

    private Captcha captcha;
    private Activity mActivity;
    public CaptchaDialog(Activity mActivity) {
        super(mActivity, R.style.BottomDialog);
        this.mActivity = mActivity;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_captcha);
        captcha = findViewById(R.id.captCha);
        captcha.setCaptchaListener(new Captcha.CaptchaListener() {
            @Override
            public String onAccess(long time) {
                Toast.makeText(mActivity,"验证成功",Toast.LENGTH_SHORT).show();
                dismiss();
                RPSDK.start("19b0169fb8a4482caccff728129499b8", mActivity,
                        new RPSDK.RPCompletedListener() {
                            @Override
                            public void onAuditResult(RPSDK.AUDIT audit, String code) {
                                if(audit == RPSDK.AUDIT.AUDIT_PASS) { //认证通过
                                    ToastUtil.showToast(mActivity, audit + "认证通过");
                                }else if(audit ==RPSDK.AUDIT.AUDIT_FAIL) { //认证不通过
                                    ToastUtil.showToast(mActivity, audit + "认证不通过");

                                }else if(audit == RPSDK.AUDIT.AUDIT_NOT) { //未认证，具体原因可通过code来区分（code取值参见下方表格），通常是用户主动退出或者姓名身份证号实名校验不匹配等原因，导致未完成认证流程
                                    ToastUtil.showToast(mActivity, audit + "未认证");
                                }
                            }
                        });
                //授权
                return "验证通过,耗时"+time+"毫秒";
            }

            @Override
            public String onFailed(int failedCount) {
                Toast.makeText(mActivity,"验证失败",Toast.LENGTH_SHORT).show();
                return "验证失败,已失败"+failedCount+"次";
            }

            @Override
            public String onMaxFailed() {
                Toast.makeText(mActivity,"验证超过次数，你的帐号被封锁",Toast.LENGTH_SHORT).show();
                return "验证失败,帐号已封锁";
            }
        });
    }
}
